package com.yidu.BusinessData.service.impl;



import com.yidu.BusinessData.entity.Fund;
import com.yidu.BusinessData.mapper.FundMapper;
import com.yidu.BusinessData.service.FundService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class FundServiceImpl implements FundService {
    @Autowired
    private FundMapper fundMapper;
    @Override
    public int insertFund(Fund fund) {
        return fundMapper.insertFund(fund);
    }

    @Override
    public List selectFund(int page, int limit, Fund fund) {
        Map fundMap =new HashMap<>();
        fundMap.put("start",(page-1)*limit);
        fundMap.put("pageRows",limit);
        fundMap.put("strWhere",buildStrWhere(fund));
        List fundList = fundMapper.selectFund(fundMap);
        return fundList;
    }

    @Override
    public int Fundtotal(Fund fund) {
        Map fundMap =new HashMap<>();
        fundMap.put("strWhere",buildStrWhere(fund));
        int total =fundMapper.Fundtotal(fundMap);
        return total;
    }

    @Override
    public int deleteFund(String fundId) {
        return fundMapper.deleteTABusiness(fundId);
    }

    @Override
    public int updateFund(Fund fund) {
        return fundMapper.updateTABusiness(fund);
    }

    /**
     * 生成SQL条件
     * @param fund 实体类
     * @return  条件的字符串
     */
    public String buildStrWhere(Fund fund){
        StringBuffer strWhere=new StringBuffer("");
        if(fund.getFundId()!=null && !fund.getFundId().equals(""))
        {
            strWhere.append(" and fundId like '%" + fund.getFundId() + "%'");
        }
        if(fund.getFundType()!=0 )
        {
            strWhere.append(" and fundType =" + fund.getFundType() );
        }
        System.out.println(strWhere);
        return strWhere.toString();
    }
}
